home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / games / IndiZone / blix / blixtexture.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.5 KB  |  178 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*________________________________________________________________________
  18.  |
  19.  | blixtexture.c - texture initialization and reading
  20.  |
  21.  |
  22.  | 1994 Frans van Hoesel, Xtreme Graphics Software
  23.  |
  24. */
  25.  
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <math.h>
  30. #include <malloc.h>
  31. #include <alloca.h>
  32. #include <gl/gl.h>
  33. #include <gl/device.h>
  34. #include <gl/image.h>
  35.  
  36. #include "blix.h"
  37. #include "blixtexture.h"
  38. #include "blixsound.h"
  39.  
  40. /* from imagelib: */
  41. IMAGE *iopen();
  42. void getrow(IMAGE *, unsigned short *, int , int);
  43. void iclose(IMAGE *);
  44.  
  45. static void readtexture(char *name, long num, int colorflag);
  46. static unsigned long *readrgbimage(char *name);
  47. static unsigned long *readbwimage(char *name);
  48.  
  49. #define RGB 1
  50. #define BW  0
  51.  
  52. void init_textures(void) {
  53.     readtexture("walls.rgb", 2, RGB);
  54.     /* readtexture("sphere.bw", 3, BW); */
  55. }
  56.  
  57. static void readtexture(char *name, long num, int colorflag) {
  58.     unsigned long *data;
  59.  
  60.     static float texps[] = {
  61.         TX_MINFILTER, TX_MIPMAP_BILINEAR,
  62.         TX_MAGFILTER, TX_BILINEAR,
  63.         TX_WRAP, TX_REPEAT,
  64.         TX_NULL};
  65.     static float tevps[] = {TV_NULL };
  66.  
  67.     if (colorflag == RGB) {
  68.     data = readrgbimage(name);
  69.     texdef2d(num, 3, 128, 128, data, 0, texps);
  70.     } else {
  71.     data = readbwimage(name);
  72.     texdef2d(num, 1, 128, 128, data, 0, texps);
  73.     }
  74.     tevdef(1, 0, tevps);
  75.     free(data);
  76. }
  77.  
  78. static unsigned long *readbwimage(char *filename) {
  79.     unsigned long *base, *lptr;
  80.     unsigned short *buf;
  81.     unsigned char *cptr;
  82.     IMAGE *image;
  83.     int x,y;
  84.     char fullpath[512];
  85.     int newx;
  86.     
  87.     strcpy(fullpath, datadir);
  88.     strcat(fullpath, filename);
  89.  
  90.     image = iopen(fullpath,"r");
  91.     if (image == NULL) {
  92.     fprintf(stderr, "%s: cannot open file '%s'\n", basename, filename);
  93.     end_sound();
  94.     gexit();
  95.     exit (1);
  96.     }
  97.     if (image->zsize != 1) {
  98.     iclose(image);
  99.     fprintf(stderr,"%s: texture file '%s' is not in BW format\n",
  100.         basename, filename);
  101.     end_sound();
  102.     gexit();
  103.     exit(1);
  104.     }
  105.     newx = image->xsize / 4;
  106.     base = (unsigned long *)malloc(newx*image->ysize*sizeof(long));
  107.     buf = (unsigned short *)alloca(image->xsize*sizeof(short));
  108.     lptr = base;
  109.     for(y=0; y<image->ysize; y++) {
  110.     cptr = (unsigned char *) lptr;
  111.     getrow(image,buf,y,0);
  112.     for (x=0; x<image->xsize; x++) {
  113.         *cptr++ = buf[x];
  114.     }
  115.     lptr += newx;
  116.     }
  117.     iclose(image);
  118.     return base;
  119. }
  120.     
  121. static unsigned long *readrgbimage(char *filename) {
  122.     unsigned long *base, *lptr;
  123.     unsigned short *rbuf, *gbuf, *bbuf;
  124.     IMAGE *image;
  125.     int newx;
  126.     unsigned char *cptr;
  127.     int x, y;
  128.     char fullpath[512];
  129.     
  130.     strcpy(fullpath, datadir);
  131.     strcat(fullpath, filename);
  132.  
  133.     image = iopen(fullpath,"r");
  134.     if (image == NULL) {
  135.     fprintf(stderr, "%s: cannot open file '%s'\n", basename, filename);
  136.     end_sound();
  137.     gexit();
  138.     exit(1);
  139.     }
  140.     if (image->zsize != 3) {
  141.     iclose(image);
  142.     fprintf(stderr,"%s: texture file '%s' is not in RGB format\n",
  143.         basename, filename);
  144.     end_sound();
  145.     gexit();
  146.     exit(1);
  147.     }
  148.     
  149.     newx = image->xsize * 3 ;
  150.     newx = newx / 4;
  151.     base = (unsigned long *)malloc(newx*image->ysize*sizeof(long));
  152.     rbuf = (unsigned short *)alloca(image->xsize*sizeof(short));
  153.     gbuf = (unsigned short *)alloca(image->xsize*sizeof(short));
  154.     bbuf = (unsigned short *)alloca(image->xsize*sizeof(short));
  155.     if(base == NULL || rbuf == NULL || gbuf == NULL || bbuf == NULL) {
  156.         fprintf(stderr, "%s: not enough memory\n", basename);
  157.     end_sound();
  158.     gexit();
  159.     exit (1);
  160.     }
  161.     lptr = base;
  162.     for(y=0; y<image->ysize; y++) {
  163.         cptr = (unsigned char *) lptr;
  164.         getrow(image,rbuf,y,0);
  165.         getrow(image,gbuf,y,1);
  166.         getrow(image,bbuf,y,2);
  167.         for (x=0; x < image->xsize; x++) {
  168.         *cptr++ = bbuf[x];
  169.         *cptr++ = gbuf[x];
  170.         *cptr++ = rbuf[x];
  171.         }
  172.         lptr += newx;
  173.     }
  174.     iclose(image);
  175.     return base;
  176. }
  177.  
  178.